﻿using System.ComponentModel;
using System.Configuration;
using System.IO;
using System.Collections.Generic;
using System;
using System.Linq;
using Citi.Helpers;

namespace Citi.SecurityHelper
{
    public enum LoginType
    {
        [Description("Active Directory")]
        ActiveDirectory,
        [Description("SSO")]
        Sso,
        [Description("SSO SIT")]
        SsoSit,
        [Description("SSO UAT")]
        SsoUat
    }

    public class CitiPasswordFile
    {
        //private static string PwdPath = ConfigurationManager.AppSettings["PwdPath"];
        private static string PwdPath = @"\\gruvnascti0016.wlb.lac.nsroot.net\DATA_GRP1\FTOOLS\Config\Encrypt";
        private static string FullPwdPath = Path.Combine(PwdPath, "{0}_{1}.pwd");

        public static string WriteFile(string soeid, string password, LoginType loginType)
        {
            var user = Encryption.Encrypt(soeid);
            var userPassword = Encryption.Encrypt(password);

            var path = string.Format(FullPwdPath, soeid.Split('\\').Last(), GetPasswordType(loginType));

            File.WriteAllLines(path, new List<string> { user, userPassword });

            return path;
        }

        private static string GetPasswordType(LoginType loginType)
        {
            switch (loginType)
            {
                case LoginType.ActiveDirectory:
                    return "AD";
                default:
                    return Enum.GetName(typeof(LoginType), loginType).ToUpper().Replace(" ", string.Empty);
            }
        }

        protected internal static IEnumerable<string> GetDecryptedText(string filename, string robotUsername, string robotPassword)
        {
            var path = Path.Combine(PwdPath, Path.HasExtension(filename) ? filename : string.Format("{0}.pwd", filename));
            var lines = GetLinesFromPwdFIle(robotUsername, robotPassword, path);
            return lines.Select(Encryption.Decrypt);
        }

        protected internal static string GetDecryptedPassword(string soeid, LoginType loginType, string robotUsername, string robotPassword)
        {
            var path = string.Format(FullPwdPath, soeid.Split('\\').Last(), GetPasswordType(loginType));
            var lines = GetLinesFromPwdFIle(robotUsername, robotPassword, path);
            return Encryption.Decrypt(lines[1]);
        }

        protected internal static IEnumerable<string> GetDecryptedUserText(string soeid, LoginType loginType, string robotUsername, string robotPassword)
        {
            var path = string.Format(FullPwdPath, soeid.Split('\\').Last(), GetPasswordType(loginType));
            var lines = GetLinesFromPwdFIle(robotUsername, robotPassword, path);
            return lines.Select(Encryption.Decrypt);
        }

        protected internal static List<string> GetLinesFromPwdFIle(string robotUsername, string robotPassword, string path)
        {
            using (var impersonate = new ImpersonateUser())
            {
                var userSplited = robotUsername.Split('\\');

                if (userSplited.Count() != 2)
                    throw new InvalidDataException(@"Username must be DOMAIN\USER.");

                impersonate.Impersonate(userSplited.First(), userSplited.Last(), robotPassword);
                
                if (!File.Exists(path))
                    throw new Exception(string.Format("File '{0}' was not found. Verify if .pwd file was generated in Citi Encrypt Tool", path));

                return File.ReadAllLines(path).ToList();
            }
        }
    }
}

